extends CharacterBody2D

const SPEED = 400
const BULLET_SCENE = preload("res://Scenes/Bullet.tscn")

@onready var fire_timer = $Timer

var is_game_over : bool = false

var attack_speed = 1
var current_attacks = 0

var touch_start_position = Vector2.ZERO
# 用于标记鼠标是否被按下
var is_mouse_pressed = false
# 存储鼠标按下时的起始位置
var mouse_start_position = Vector2.ZERO
# 存储角色在鼠标按下时的起始位置
var sprite_start_position = Vector2.ZERO

# 跟随角色的数组
var followers = []
const MAX_FOLLOWERS = 6

var currentFollowerX = 30

func _input(event):
	# 处理鼠标按下事件
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT:
			if event.pressed:
				# 标记鼠标已按下
				is_mouse_pressed = true
				# 记录鼠标按下时的位置
				mouse_start_position = event.position
				# 记录角色在鼠标按下时的位置
				sprite_start_position = position
			else:
				# 标记鼠标已释放
				is_mouse_pressed = false
	# 处理鼠标移动事件
	elif event is InputEventMouseMotion and is_mouse_pressed:
		# 计算鼠标移动的偏移量
		var mouse_offset = event.position - mouse_start_position
		# 更新角色的位置
		position = sprite_start_position + mouse_offset
	elif event is InputEventScreenTouch:
		if event.pressed:
			touch_start_position = event.position
		else:
			touch_start_position = Vector2.ZERO
	elif event is InputEventScreenDrag:
		var drag_offset = event.position - touch_start_position
		var new_position = position + drag_offset
		
		# 获取屏幕的边界
		var screen_size = get_viewport_rect().size
		new_position.x = clamp(new_position.x, 0, screen_size.x)
		new_position.y = clamp(new_position.y, 0, screen_size.y)
		
		position = new_position
		touch_start_position = event.position

func _ready():
	add_to_group("player")
	attack_speed = $"../PlayerManager".attack_speed
	fire_timer.wait_time = 1.0/attack_speed
	fire_timer.start()

func _physics_process(delta):
	# 移动控制
	var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	velocity = input_dir * SPEED
	if attack_speed != $"../PlayerManager".attack_speed:
		attack_speed = $"../PlayerManager".attack_speed
		fire_timer.wait_time = 1.0 / attack_speed
	move_and_slide()
	
	# 边界限制
	#position = position.clamp(Vector2(0, 0), get_viewport_rect().size)
		

func shoot():
	if not is_game_over:
		var bullet = BULLET_SCENE.instantiate()
		bullet.position = position + Vector2(0, -30)
		get_parent().add_child(bullet)
		for value in followers:
			value.shoot()
			
	
func game_over():
	if not is_game_over:
		is_game_over = true
		$"../over".visible=true
		await get_tree().create_timer(3).timeout
		get_tree().reload_current_scene()
		#await get_tree().create_timer(1).timeout
		#GameManager.reset()
		#ItemManager.reset()
		#PlayerManager.reset()

func _on_timer_timeout_Fire() -> void:
	shoot()


func spawn_follower():
	if followers.size() < MAX_FOLLOWERS:
		var follower = preload("res://Scenes/FollowerPlayer.tscn").instantiate()
		# 计算偏移量
		var count = followers.size() + 1
		var tmpX = currentFollowerX
		if count % 2 == 1:
			tmpX = -currentFollowerX
		else:
			currentFollowerX += 30
			tmpX = currentFollowerX
		print(tmpX)
		var offset = Vector2(tmpX, 0)  # 距离主玩家100像素
		follower.follow_offset = offset
		get_parent().add_child(follower)
		followers.append(follower)
